package com.longdaji.decoration.ui.activitiesOfMine.refund.forRefund;

import android.text.TextUtils;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/4/10 0010.
 */

public class ForRefundPresenter extends RxPresenter<ForRefundContract.IView> implements ForRefundContract.IPresenter {
    private DataManager mDataManager;
    private Account account;

    @Inject
    public ForRefundPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
        account = Account.getInstance();
    }


    @Override
    public void onFinishClick(String orderNo, int skuId, int goodsNum, double money, String reason, String description) {
        addSubscribe(mDataManager.doReturnGoodsApi(account.getUserid(), orderNo, skuId, goodsNum, 0, money, reason, description, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.i("success doReturnGoodsApi");
                        mView.showRefundSucceed();
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.i("fail doReturnGoodsApi: " + msg);
                    }
                }));
    }

    @Override
    public void initData(String orderNo, final int goodsId) {
        addSubscribe(mDataManager.doGetOrderApi(Account.getInstance().getUserid(), null, orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Orders>handleResult())
                .subscribeWith(new CommonSubscriber<Orders>() {
                    @Override
                    public void onSuccess(Orders orders) {
                        LogUtil.d("succeed : doGetOrder ");
                        List<Orders.OrdersBean.OrderBean.ProductsBean> productList = orders.getOrders().getOrder().get(0).getProducts();
//                        mView.showForRefundOrderInfo(orders.getOrders().getOrder().get(0));
                        for (Orders.OrdersBean.OrderBean.ProductsBean productsBean : productList) {
                            if (productsBean.getGoodsId() == goodsId) {
                                mView.showForRefundProductInfo(productsBean);
                                return;
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);

                    }
                }));
    }
}
